﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Engine.Components
{
    public class SceneManager:BaseManager
    {
        Dictionary<string, Func<NOWAEngine,Scene.IBaseScene>> _Scenes;
      
        Scene.IBaseScene _CurrentScenes;        
        public SceneManager(NOWAEngine _game, ComponentManager manager, bool nodel = false)
            : base(_game, manager, nodel)
        {
            // TODO: Complete member initialization
            _Scenes = new Dictionary<string, Func<NOWAEngine, Scene.IBaseScene>>(0);
            //_CurrentScenes = new Scene.BaseScene(_game, "Base",true);

         //   _Scenes.Add("Base",baseScene);
          //  _CurrentScenes = _Scenes["Base"].Invoke(_game);
          // switchScene("Base");
        }
        public Scene.IBaseScene baseScene(NOWAEngine _game)
        {
            return new Scene.BaseScene(_game, "Base", true);
        }
        public void addObject(Interface.IEngineObject obj,Scene.SceneNode parent=null)
        {
            if (_CurrentScenes != null)
            {
                _CurrentScenes.addObject(obj,parent);
            }
        }
        public dynamic getGUIByName(string name)
        {
            if (_CurrentScenes != null)
            {
                return _CurrentScenes.getGUIByName(name);
            }
            return null;
        }
        public dynamic getObjectByName(string name)
        {
            if (_CurrentScenes != null)
            {
                return _CurrentScenes.getObjectByName(name);
            }
            return null;
        }
        public Scene.IBaseScene CurrentScene
        {
            get
            {
                return _CurrentScenes;
            }
        }
        public void addScene(String Name, Func<NOWAEngine, Scene.IBaseScene> obj)
        {
            if (!_Scenes.ContainsKey(Name))
            {
                _Scenes.Add(Name, obj);
            }
        }
        public void RemoveScene(string name)
        {
            if (_Scenes.ContainsKey(name))
            {
                if (_CurrentScenes.Name == name)
                {

                }
                else
                {
                    _Scenes.Remove(name);
                }
            }
        }
        public void switchScene(string nameScene)
        {
            if (_Scenes.ContainsKey(nameScene))
            {
                _CurrentScenes = _Scenes[nameScene].Invoke((NOWAEngine)Game);
                _CurrentScenes.LoadContent();
                _CurrentScenes.Initialize();
            }
        }
        public override void Initialize()
        {
            if (_CurrentScenes!=null)
            {
                 _CurrentScenes.Initialize();
            }
           
            base.Initialize();
        }
        public override void Update(GameTime gameTime)
        {
            if (_CurrentScenes != null)
            {
                _CurrentScenes.Update(gameTime);
            }
            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            if (_CurrentScenes != null)
            {
                _CurrentScenes.Draw(gameTime);
            }
              base.Draw(gameTime);
             

        }
        protected override void LoadContent()
        {
            if (_CurrentScenes != null)
            {
                _CurrentScenes.LoadContent();
            }
            base.LoadContent();
        }        
    }
}
